<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .step-number{
            display: flex;
            font-size: 14px;
        }

        .step-number .step-reduce,
        .step-number .step-add{
            width: 40px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: grey;
        }

        .step-number .step-num{
            height: 40px;
            width: 120px;
            box-sizing: border-box;
            outline: none;
            /* text-indent: 10px; */
            text-align: center;
            border-radius: 0;
            border: 2px solid #333;
        }
        .disabled{
            cursor: not-allowed;
        }

        .step-1{
            margin: 20px;
        }
       
    </style>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>

<body>
    <div id="app">
        
        <p>
            数量: {{num}}
        </p>
        <p>
            <button @click="num--">num--</button>
            <button @click="num++">num++</button>
        </p>

        <hr>
        
        <!-- <laowang-step-number class="step-1" :count="num" @update:count="v=>num=v"></laowang-step-number> -->

        <!-- Vue3 没有.sync修饰符  而是使用v-model代替 -->
        <laowang-step-number class="step-1" v-model:count="num" ></laowang-step-number>
    </div>
</body>
<script>

    /* 

        Vue3 对比 Vue2?
            1. Vue3中的 Vue是一个对象 => 存储各种工具方法
            2. 创建根组件需要借助createApp方法   
            3. 选项式写法中不在支持 el选项, 而是选用先创建,在挂载的写法(app.mount())
            4. Vue3所有组价的data都要求是返回对象的函数 
            5. 全局组件注册 app.component(), 需要在根组件创建之后, 挂载之前注册
            6. 数据 方法 计算属性 不会直接存储到Vue实例上, 而是单独存储到一个Proxy代理对象上

      

    */

    /* let LaowangStepNumber = {
        props:{
            count:{
                type:Number,
                default:1,
            }
        },
        template:`<div class="step-number">
            <div class="step-reduce" :class="{disabled: count <= 1}" @click="reduceCount">-</div>
            <input class="step-num" type="text"  :value="count" >
            <div class="step-add" @click="addCount">+</div>
        </div>`,
        methods:{
            reduceCount(){
                if(this.count <= 1) return false;
                this.$emit("update:count",this.count - 1);
            },
            addCount(){
                this.$emit("update:count",this.count + 1);
            }
        },
        mounted () {
            console.log("子组件Vue实例",this);
        }

    } */
    
    // console.log(Vue);
    let {createApp} = Vue;

    let app = createApp({
        // el:""
        data(){
            return {
                num:1,
            }
        },
        methods:{
            updateNum:function(val){  // 根组件挂载的函数 (this=>根组件)
                this.num = val;
            }
        },
        // components: {
        //     LaowangStepNumber,
        // }
    })
   


    app.component("LaowangStepNumber",{
        props:{
            count:{
                type:Number,
                default:1,
            }
        },
        template:`<div class="step-number">
            <div class="step-reduce" :class="{disabled: count <= 1}" @click="reduceCount">-</div>
            <input class="step-num" type="text" :value="count">
            <div class="step-add" @click="addCount">+</div>
        </div>`,
        methods:{
            reduceCount(){
                if(this.count <= 1) return false;
                this.$emit("update:count",this.count - 1);
            },
            addCount(){
                this.$emit("update:count",this.count + 1);
            }
        },
        mounted () {
            console.log("子组件Vue实例",this);
        }

    })


    console.log(app);
    app.mount("#app");





  


    





</script>

</html>